/**
 *   Copyright 2008 Alexey Vishentsev
 *
 *   Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

package com.gwtspreadsheetinput.gwt.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Grid;

public class SelectionManager extends AbsolutePanel{

	private AbsolutePanel selection;
	public Grid table;
	public int selectedRow = -1;
	public int selectedCol = -1;
	private InputManager inputManager;
	private DynamicGrid dynamicGrid;
	
	public SelectionManager() {
		super();
	    //Init selection
		addStyleName("spreadsheet-Selection-container");
		setPixelSize(0, 0);
		DOM.setStyleAttribute(getElement(), "overflow", "visible");
		//Init selection
		selection = new AbsolutePanel();
		selection.addStyleName("spreadsheet-Table-selection");
		selection.setPixelSize(0, 0);
		selection.setVisible(false);
		add(selection,0,0);
	}
	
	public void setDynamicGrid(DynamicGrid dynamicGrid) {
		this.dynamicGrid = dynamicGrid;
		this.table = dynamicGrid.mainGrid;
		this.inputManager = dynamicGrid.inputManager;
	}
	
	public void updateSelectionGeometry(){
		if (selectedRow < 0 || selectedCol < 0) {
			selection.setVisible(false);
			return;
		}
		//This method assumes table is contained in SelectionManager's parent
		//And SelectionManager is located in position 0,0 in it's parent
		int [] rect = new int[4];
		Util.getSelecitonRectangle(this.getElement().getParentElement(), table, 
				selectedRow, selectedCol, rect);
		
		setWidgetPosition(selection, rect[0]-1, rect[1]-1);
		selection.setPixelSize(rect[2]-2,rect[3]-2);
		selection.setVisible(true);
	}
	
	public void setSelection(int row, int col){
		if (row == selectedRow && col == selectedCol) {
			return;
		}
		if (row < 0 || row >= table.getRowCount() ||
		    col < 0 || col >= table.getColumnCount()) {
			selectedRow = -1;
			selectedCol = -1;
		} else {
			selectedRow = row;
			selectedCol = col;
			inputManager.editor.setFocus(true);
			dynamicGrid.scrollToCell(selectedRow, selectedCol);
		}
		
		dynamicGrid.saveSelection(selectedRow, selectedCol);
		inputManager.stopEditing(true);
		updateSelectionGeometry();
	}
	
	
	public boolean rightNavigation() {
		if (selectedCol < table.getColumnCount() - 1) {
			setSelection(selectedRow, selectedCol + 1);
		}
		return true;
	}

	public boolean leftNavigation() {
		if (selectedCol > 0) {
			setSelection(selectedRow, selectedCol - 1);
		}
		return true;
	}

	public boolean upNavigation() {
		if (selectedRow > 0) {
			setSelection(selectedRow-1, selectedCol);
		}
		return true;
	}

	public boolean enterNavigation() {
		if (selectedRow < table.getRowCount() - 1) {
			setSelection(selectedRow+1, selectedCol);
		}
		return true;
	}
	
	public boolean downNavigation() {
		if (selectedRow < table.getRowCount() -1) {
			setSelection(selectedRow+1, selectedCol);
		}
		return true;
	}

	public boolean tabNavigation(boolean withShift) {
		if (withShift) {
			if (selectedCol > 0) {
				setSelection(selectedRow, selectedCol -1);
			} else if (selectedRow > 0) {
				setSelection(selectedRow -1, table.getColumnCount() -1);
			} else {
				setSelection(table.getRowCount() -1, table.getColumnCount() -1);
			}
		} else {
			if (selectedCol < table.getColumnCount() - 1) {
				setSelection(selectedRow, selectedCol + 1);
			} else if (selectedRow < table.getRowCount() -1) {
				setSelection(selectedRow+1, 0);
			} else {
				setSelection(0, 0);
			}
		}
		return true;
	}
}
